package com.xyd.Servlet;

import com.xyd.Biz.Impl.UserRegBizImpl;
import com.xyd.Biz.UserRegBiz;
import com.xyd.Entity.User;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UserRegServlet")
public class UserRegServlet extends HttpServlet {

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("userName");
        String pwd = request.getParameter("passWord");
        String power = request.getParameter("poWer");
        String realname = request.getParameter("userName");
        String realnnumber = request.getParameter("num");
        String birthyear = request.getParameter("birthyear");
        String birthmonth = request.getParameter("birthmonth");
        String birthday = request.getParameter("birthday");
        String phonenum = request.getParameter("mobile");
        String addressid = request.getParameter("address");
        String sex = request.getParameter("sex");

        String birth = birthyear + "-" + birthmonth + "-" + birthday;

        UserRegBiz userRegBiz = new UserRegBizImpl();
        User u = new User(0, username, pwd, Integer.parseInt(power), realname, realnnumber, birth, sex, phonenum, addressid);
        User user = userRegBiz.UserReg(u);

        if (user != null) {
            request.getSession().setAttribute("user", user);
            request.getRequestDispatcher("reg-result.jsp").forward(request, response);
        } else {
            response.sendRedirect("register.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
